﻿using System;
using System.Collections;
using System.Web.UI;
using Demo.CommonBase.View.Contracts;
using Demo.CommonBase.View.Controllers;
using Demo.CommonBase.Helpers;
using System.Globalization;


namespace Demo.CommonBase.View.Forms
{
    public class PageBase : Page, IView
    {
        private readonly Hashtable values = new Hashtable();
        private string controllerName;
        private AbstractDemoController abstractDemoController;

        #region IView Members
        public string GetFormatDate(string date)
        {
            string returnValue;
            CultureInfo culture = new CultureInfo("es-MX");
            returnValue = DateTime.Parse(date, culture).ToString("yyyy-MM-dd");           
            return returnValue;
        }

        public object GetValue(string key)
        {
            object result = null;
            if (!string.IsNullOrEmpty(key))
                result = values[key];

            return result;
        }

        public void SetValue(string key, object value)
        {
            values[key] = value;
        }

        public string ControllerName
        {
            get { return controllerName; }
            set { controllerName = value; }
        }


        public AbstractDemoController AbstractATSController
        {
            get
            {
                return abstractDemoController;
            }
        }

        #endregion


        
        private static AbstractDemoController GetController(string controllerView)
        {
            return (AbstractDemoController)FactoryHelper.GetObject(controllerView);
        }

          protected override void OnLoad(EventArgs e)
          {
              if (!string.IsNullOrEmpty(controllerName))
              {
                  abstractDemoController = GetController(controllerName);
              }
              base.OnLoad(e);
          }
    }
}
